package com.shop.dao.imp;

import com.shop.beans.User;
import com.shop.dao.UserDao;
import com.shop.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class UserDaoImp01 implements UserDao {

    @Override
    public void addUser(User user) throws ParseException {
        Date date1=user.getBirthday();
        java.sql.Date startDate=new java.sql.Date(date1.getTime());
        try {
            Connection connection=new JDBCUtil().getConnection();
            String sql="insert into t_user values(null,?,?,?,?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3,user.getRealname());
            ps.setString(4,user.getCardid());
            ps.setDate(5, startDate);
            ps.setString(6,user.getPhone());
            ps.setString(7,user.getUserInfo());
            ps.execute();
            System.out.println("【INFO】=>ADD-[ACC]添加用户成功\n\t\t->|"+user);
            connection.close();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateUser(User user) {
        java.sql.Date startDate=new java.sql.Date(user.getBirthday().getTime());
        try {
            int id=user.getId();
            Connection connection= JDBCUtil.getConnection();
            String sql="update t_user set  username=? , password=? , realname=? , cardid=? , birthday=? , phone=? , userInfo=? where id=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3,user.getRealname());
            ps.setString(4,user.getCardid());
            ps.setDate(5, startDate);
            ps.setString(6,user.getPhone());
            ps.setString(7,user.getUserInfo());
            ps.setInt(8,id);
            ps.execute();
            System.out.println("【INFO】=>UP-[ACC]修改id为"+id+"用户名为"+user.getUsername()+"的用户成功\n\t\t->|"+user);
            connection.close();

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    //删除用户
    @Override
    public void deleteUser(int id) throws ParseException {
        try{
                Connection connection= JDBCUtil.getConnection();
                String sql="delete  from t_user where id=?";
                PreparedStatement ps=connection.prepareStatement(sql);
                ps.setInt(1,id);
                ps.execute();
            System.out.println("【INFO】=>UP-[ACC]修改id为"+id+"用户名为"+"的用户成功\n\t\t->|");
            connection.close();
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
    }

    @Override
    public List<User> getUsers() {
        List<User> users=new ArrayList<User>();
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="select * from t_user";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet resultSet= ps.executeQuery();
            //ResultSet resultSet= executeQuery(sql);
            System.out.println("【INFO】=>GetUsers-[ACC]获取用户成功");
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                String username=resultSet.getString("username");
                String password=resultSet.getString("password");
                String realname=resultSet.getString("realname");
                String cardid=resultSet.getString("cardid");
                Date birthday=resultSet.getDate("birthday");
                String phone=resultSet.getString("phone");
                String userInfo=resultSet.getString("userInfo");
                User user=new User(id,username,password,realname,cardid,birthday,phone,userInfo);
                users.add(user);
                System.out.println("\t\t->|"+user);
            }
            System.out.println("\t\t->|共计"+users.size());
            connection.close();
            return users;
            //ps.execute();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public User getUserById(int id) {
        User user1=new User();
        try{
            Connection connection=JDBCUtil.getConnection();
            String sql="select * from t_user where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet resultSet=ps.executeQuery();
            while (resultSet.next()){
                String username=resultSet.getString("username");
                String password=resultSet.getString("password");
                String realname=resultSet.getString("realname");
                String cardid=resultSet.getString("cardid");
                Date birthday=resultSet.getDate("birthday");
                String phone=resultSet.getString("phone");
                String userInfo=resultSet.getString("userInfo");
                User user=new User(id,username,password,realname,cardid,birthday,phone,userInfo);
                user1=user;
                connection.close();
                break;
            }
            if (user1.getUsername()==null&&user1.getPassword()==null&&user1.getRealname()==null&&user1.getCardid()==null){
                System.out.println("【INFO】=>GetUsers-[FAL]获取用户id为"+id+"失败");
                return null;
            }
            System.out.println("【INFO】=>GetUsers-[ACC]获取用户成功\n\t\t->|"+user1);
            return user1;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }




}
